package com.netease.xone.widget;


import com.netease.xone.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;

/**
 * <p>
 * 文字闪光效果的TextView
 * </p>
 * <p>
 * 基本用法同TextView,使用setFlashXXX设置闪光效果
 * </p>
 * <p>
 * 暂不支持gravity参数
 * </p>
 * <p>
 * 使用文字阴影必须代码调用setShadowLayer,在xml设置shadow参数无效
 * </p>
 *
 * @author MR
 */
public class FlashTextView extends TextView {

    public static final int DIRECTION_RIGHT_TO_LEFT = 0;
    public static final int DIRECTION_LEFT_TO_RIGHT = 1;

    private TextPaint mTextPaint;
    private Paint mFlashPaint;
    private Scroller mScroller;
    private Bitmap mFlashBitmap;
    private int mAnimTime;
    private Rect mFlashRect;
    private int mFlashDirection = DIRECTION_RIGHT_TO_LEFT;
    private boolean mIsFlashShaowJump = false;// 有文字阴影时，是否使用跳动效果的闪烁
    // 文字阴影相关
    private float mShadowRadius;
    private float mShadowDx;
    private float mShadowDy;
    private int mShadowColor;

    public FlashTextView(Context context) {
        this(context, null);
    }

    public FlashTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.density = getResources().getDisplayMetrics().density;
        mFlashPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFlashPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
        mScroller = new Scroller(context, new LinearInterpolator());
        mFlashRect = new Rect();
        // set default
        mAnimTime = 2000;
        mFlashBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_text_flash);
    }

    @Override
    public void setShadowLayer(float radius, float dx, float dy, int color) {
        mShadowRadius = radius;
        mShadowDx = dx;
        mShadowDy = dy;
        mShadowColor = color;
        mTextPaint.setShadowLayer(radius, dx, dy, color);
    }

    public void setFlashResource(int resId) {
        mFlashBitmap = BitmapFactory.decodeResource(getResources(), resId);
    }

    public void setFlashBitmap(Bitmap bitmap) {
        mFlashBitmap = bitmap;
    }

    /**
     * 设置一次闪光动画时间
     *
     * @param animTime
     */
    public void setFlashDuration(int animTime) {
        mAnimTime = animTime;
    }

    /**
     * 设置闪光方向
     *
     * @param direction
     * @see DIRECTION_LEFT_TO_RIGHT
     * @see DIRECTION_RIGHT_TO_LEFT
     */
    public void setFlashDirection(int direction) {
        mFlashDirection = direction;
    }

    /**
     * 设置文字带阴影时是否使用跳动效果的闪光
     *
     * @param isJump
     */
    public void setFlashShadowJump(boolean isJump) {
        mIsFlashShaowJump = isJump;
    }

    private void startAnim() {
        switch (mFlashDirection) {
            case DIRECTION_LEFT_TO_RIGHT:
                mScroller.startScroll(-getMeasuredWidth(), 0,
                        getMeasuredWidth() + 2 * mFlashBitmap.getWidth(), 0, mAnimTime);
                break;
            case DIRECTION_RIGHT_TO_LEFT:
            default:
                mScroller.startScroll(getMeasuredWidth() + mFlashBitmap.getWidth(), 0,
                        -(getMeasuredWidth() + 2 * mFlashBitmap.getWidth()), 0, mAnimTime);
                break;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (hasShadow() && !mIsFlashShaowJump) {
            mTextPaint.setShadowLayer(mShadowRadius, mShadowDx, mShadowDy, mShadowColor);
            canvas.drawText(getText().toString(), 0, getTextSize(), mTextPaint);
        }
        canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
        mTextPaint.setTextSize(getTextSize());
        mTextPaint.setColor(getTextColors().getDefaultColor());
        if (hasShadow() && !mIsFlashShaowJump) {
            mTextPaint.setShadowLayer(0, 0, 0, 0);
        }
        canvas.drawText(getText().toString(), 0, getTextSize(), mTextPaint);
        if (mScroller.computeScrollOffset()) {
            int x = mScroller.getCurrX();
            mFlashRect.set(x, 0, mFlashBitmap.getWidth() + x, getMeasuredHeight());
            canvas.drawBitmap(mFlashBitmap, null, mFlashRect, mFlashPaint);
        } else {
            startAnim();
        }
        canvas.restore();
        invalidate();
    }

    private boolean hasShadow() {
        return mShadowRadius != 0;
    }
}
